package mk.com.ldm.corepoint.implementation.client.lib.wrappers.widgets;

import gwtupload.client.IFileInput.FileInputType;
import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;
import gwtupload.client.PreloadedImage.OnLoadPreloadedImageHandler;
import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseWidget;
import mk.com.ldm.corepoint.implementation.shared.FormRow;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;




public class UploadWrapper extends BaseWidget{

	private Label label = new Label();
	private MultiUploader uploader = new MultiUploader(FileInputType.BUTTON);
	private HorizontalPanel panelImages = new HorizontalPanel();
	String imageSize = new String();
	String fileName = new String();
	
	public UploadWrapper(FormRow config, HandlerManager eventBus) {
		super(config);
		
		this.setType(8);
		label.setText(config.getNam());
		label.setWidth(this.LABEL_WIDTH);
		
		imageSize = config.getSiz();
		
		uploader.setTitle(config.getNam().replace(":",""));
		uploader.addOnFinishUploadHandler(onFinishUploaderHandler);
		uploader.setStatusWidget(null);
		uploader.ensureDebugId("uploader");
		
		
		FlexTable layout = new FlexTable();
		
		layout.setWidget(0, 0, label);
		layout.setWidget(0, 1, uploader);
		ScrollPanel sp = new ScrollPanel();
		sp.setStyleName("thumbs");
		sp.setHeight("140px");
		sp.setWidth("400px");
		sp.add(panelImages);
		panelImages.setHeight(imageSize.split(",")[1]+"px");
		panelImages.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		panelImages.setSpacing(5);
		layout.setWidget(1, 1, sp);	
		layout.setHeight("100px");
		layout.setStyleName("uploadPanel");
		initWidget(layout);
	}

	
	private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
	    public void onFinish(IUploader uploader) {
	    	DOM.getElementById("gwt-debug-uploader").removeChild(DOM.getElementById("gwt-debug-uploader").getFirstChild());
	        if (uploader.getStatus() == Status.SUCCESS) {
	        	fileName = uploader.getBasename();
	        	new PreloadedImage(uploader.fileUrl(), showImage);
	        // The server can send information to the client.
	        // You can parse this information using XML or JSON libraries
//	        Document doc = XMLParser.parse(uploader.getServerResponse());
//	        String size = Utils.getXmlNodeValue(doc, "file-1-size");
//	        String type = Utils.getXmlNodeValue(doc, "file-1-type");
//	        System.out.println(size + " " + type);
	      }
	    }
	  };

	  // Attach an image to the pictures viewer
	  private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
	    public void onLoad(PreloadedImage image) {
	      image.setWidth(imageSize.split(",")[0]+"px");
	      image.setHeight(imageSize.split(",")[1]+"px");
	      VerticalPanel vp = new VerticalPanel();
	      vp.setStyleName("thumbnail");
	      vp.add(image);
	      Label file = new Label(fileName);
	      file.setWidth(imageSize.split(",")[0]+"px");
	      vp.add(file);
	      panelImages.add(vp);
	    }
	};
}
